<?php
namespace Manage\Controller;

use Manage\Controller\BaseController;
use Think\Controller;

class FeedbackController extends BaseController
{
    public $obj;

    public function __construct()
    {
        parent::__construct();
        $this->obj = M('b2c_member_feedback');
        $this->_arr['title'] = '投诉与建议';

    }

    public function index()
    {
        $this->_arr['adm_status'] = I('adm_status');
        $this->_arr['title_two'] = '投诉与建议列表';
        $this->_arr['p'] = I('p', 1, 'intval');

        $this->_arr['select'] = I('select');
        $this->_arr['value'] = trim(I('value'));
        $this->_arr['begintime'] = I('begintime');
        $this->_arr['endtime'] = I('endtime');
        $where = '1=1';
        if($this->_arr['begintime']){
            $begintime = strtotime($this->_arr['begintime']);
            $where .= " and `time` >= '".$begintime."'";
        }
        if($this->_arr['endtime']){
            $endtime = strtotime($this->_arr['endtime']) + 86399;
            $where .= " and `time` < '".$endtime."'";
        }
        if($this->_arr['adm_status']){
            $where .= " and adm_status = '".$this->_arr['adm_status']."'";
        }

        if($this->_arr['select'] && $this->_arr['value']){
            switch($this->_arr['select']){
                case 'id' :
                    $where .= ' and feedback_id = '.$this->_arr['value'];
                    break;
                case 'name' :
                    $where .= " and author like '".$this->_arr['value']."%'";
                    break;
            }
        }

        $order_id = I('id') ? I('id') : 2;
        if ($order_id == 1) {
            $order = 'feedback_id asc';
            $this->_arr['rank'] = 2;
        } elseif ($order_id == 2) {
            $order = 'feedback_id desc';
            $this->_arr['rank'] = 1;
        }
        $data = $this->obj->where($where)->order($order)->page($this->_arr['p'] . ','.$this->page_num)->select();
        //echo M()->_sql();
        $count = $this->obj->where($where)->order($order) ->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, $this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出

        $this->_arr['page'] = $show;// 赋值分页输出
        $this->_arr['list'] = $data;
        $this->_showDisplay();
    }


    /**
     * 处理
     */
    public function saveFeedback(){
        $id = I('id') or $this->error('参数为空！');
        if (!is_numeric($id)) $this->error('无效参数！');
        $this->_arr['title_two'] = '回复';
        $this->_arr['id'] = $id;
        $this->_showDisplay();
    }
    public function doSaveFeedback()
    {
        $id = I('id') or $this->error('参数为空！');
        if (!is_numeric($id)) $this->error('无效参数！');
        $admin_id = session('manageInfo')['account_id'];
        $content = I('content');
        $subject = I('subject');
        $revert_data = [
            'feedback_id' => $id,
            'admin_id' => $admin_id,
            'subject' => $subject,
            'content' => $content,
            'create_time' => time(),
        ];
        $model = M();
        try{
            //入回复表！
            $revertStat = M('b2c_admin_revert')->add($revert_data);
            if($revertStat === false){
                throw new \Exception(false);
            }
            //修改反馈表状态
            $feedback_stat = M('b2c_member_feedback')->where(['feedback_id' => $id])->save(['adm_status'=>'true']);
            if($feedback_stat === false){
                throw new \Exception(false);
            }
            // 提交事务
            $model->commit();
            $this->success('处理成功', 'index');
        }catch (\Exception $e){
            // 事务回滚
            $model->rollback();
            $this->error('处理失败', 'index');
        }

    }

    public function __destruct()
    {
        unset($this->obj);
    }


}